//
//  YbzcVC.swift
//  Auscultation
//
//  Created by 来 on 2020/12/22.
//

import UIKit

class YbzcVC: BaseVC {
    
    var collectionView: UICollectionView!
    
    var dataArray: [[String: Any]] = []
    var pageNum: Int = 1
    var pageSize: Int = 10
    var keyWord: String = ""
    var status: String = ""
    
    var sx_state: [Int] = [0]

    override func viewDidLoad() {
        super.viewDidLoad()

        initUI()
        collectionView.mj_header = MJRefreshNormalHeader.init(refreshingTarget: self, refreshingAction: #selector(loadData))
        collectionView.mj_footer = MJRefreshAutoNormalFooter.init(refreshingTarget: self, refreshingAction: #selector(loadMoreData))
        loadData()
    }
    
    // 数据刷新
    @objc func loadData() {
        pageNum = 1
        dataArray.removeAll()
        loadMoreData()
    }
    @objc func loadMoreData() {
        let paramet: [String: String] = [
            "keyWord" : keyWord,
            "status": status,
            "pageNo" : pageNum.description,
            "pageSize" : pageSize.description
        ]
        EWERequest.lgf("arbitration", paramet, .get) { [weak self] (data) in
            let result = (data["result"] as? [[String: Any]]) ?? []
            if result.count > 0 {
                self?.dataArray += result
                self?.pageNum += 1
            }
            self?.collectionView.reloadData()
            self?.endRefreshing()
            if result.count == 0 {
                self?.collectionView.mj_footer?.endRefreshingWithNoMoreData()
            }
        } _: { [weak self] (error) in
            SVProgressHUD.showError(error)
            self?.endRefreshing()
        }
    }
    func endRefreshing() {
        self.collectionView.mj_header?.endRefreshing()
        self.collectionView.mj_footer?.endRefreshing()
    }
    
    @objc func s_sx() {
        SxV().showIn(self.view, ["状态"], ["状态": ["未仲裁", "已仲裁"]], self.sx_state) { [weak self] (sxv, name, state) in
            if state[0] == 0 {
                self?.status = ""
            } else if state[0] == 1 {
                self?.status = "10"
            } else if state[0] == 2 {
                self?.status = "20"
            }
            if self?.sx_state[0] != state[0] {
                self?.loadData()
            }
            self?.sx_state[0] = state[0]
        }
    }

}

extension YbzcVC: UITextFieldDelegate {
    func textFieldDidEndEditing(_ textField: UITextField) {
        keyWord = textField.text ?? ""
        loadData()
    }
}

extension YbzcVC : UICollectionViewDelegateFlowLayout, UICollectionViewDelegate, UICollectionViewDataSource, UIScrollViewDelegate {
    
    func scrollViewDidScroll(_ scrollView: UIScrollView) {
        NotificationCenter.default.post(name: NSNotification.Name(rawValue: "YbzcCellPlayerStop"), object: nil)
    }
    
    func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize  {
        return CGSize.init(width: UIDevice.lgf_ScreenW, height: 140)
    }
    func numberOfSections(in collectionView: UICollectionView) -> Int {
        return 1
    }
    func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int {
        return dataArray.count
    }
    func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
        let cell = collectionView.lgf_DequeueReusableCell(with: YbzcCell.self, for: indexPath)
        cell.tzzx.text = ((dataArray[indexPath.item]["sample"] as? [String: Any])?["no"] as? String) ?? ""
        cell.cjsj.text = "创建时间：" + (((dataArray[indexPath.item]["createdTime"] as? Int) ?? 0).description.lgf_TimeStampToDate().lgf_TimeStr("yyyy-MM-dd HH:mm:ss"))
        cell.tw.text = "体位：" + (((dataArray[indexPath.item]["sample"] as? [String: Any])?["position"] as? String) ?? "")
        cell.phcd.text = "配合程度：" + (((dataArray[indexPath.item]["sample"] as? [String: Any])?["coordinate"] as? String) ?? "")
        cell.url = ((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["wavPath"] as? String) ?? ""
        cell.initPlay()
        cell.allTime = cell.player.getAllTime(((dataArray[indexPath.item]["sampleDetail"] as? [String: Any])?["duration"] as? Int) ?? 0)
        cell.play_time.text = "00:00/" + cell.allTime
        let status = (dataArray[indexPath.item]["status"] as? Int) ?? 10
        // 仲裁状态：10：未仲裁；20：已仲裁
        if status == 10 {
            cell.status.text = "正在仲裁"
            cell.status.textColor = UIColor.init(lgf_HexString: "8D969D")
        } else if status == 20 {
            cell.status.text = "仲裁完成"
            cell.status.textColor = UIColor.init(lgf_HexString: "0F88F2")
        }
        return cell
    }
    
    func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        let vc = BzVC()
        let status = (dataArray[indexPath.item]["status"] as? Int) ?? 0
        // 仲裁状态：10：未仲裁；20：已仲裁
        if status == 20 {
            vc.type = 5
        } else {
            vc.type = 6
        }
        vc.stype = 3
        vc.id = (dataArray[indexPath.item]["sampleId"] as? String) ?? ""
        vc.arbitrationId = (dataArray[indexPath.item]["id"] as? String) ?? ""
        self.navigationController?.show(vc, sender: self)
    }
}
